package com.sinodata.bsm.cicp.finder.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.mysql.MySQLContants;
import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: ylchkj</p>
 * @author not attributable
 * @version 1.0
 *
 * MySQL Storage 监控 ，
 * 1.logs ,
 */
public class MySQLStorage extends Finder {
    private static final Logger logger = Logger.getLogger(MySQLStorage.class);
    private static final String SqlCmd = "show variables like '%data%'";

    public MySQLStorage() {
    }

    private Map<String, String> queryStorage(String ip, String serverName) {
        Map<String, String> dbMap = new HashMap<String, String>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = DBConnectionUtil.getInstance().getConnection("mysql", ip, serverName);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(SqlCmd);
            while (rs.next()) {
                dbMap.put(rs.getString(1).toLowerCase(), rs.getString(2) == null ? "" : rs.getString(2).trim().toLowerCase());
            }
        } catch (Exception ex) {
            logger.error("Find mysql(" + serverName + ") mysqlstorage error", ex);
        } finally {
            if (null != rs) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    logger.error("failed to close resultSet", e);
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    logger.error("failed to close statement", e);
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }

        return dbMap;
    }

    private String[] getDbStorageKeys() {
        return new String[] { "datadir", "innodb_data_file_path", "innodb_data_home_dir" };
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String serverName = (String) params.get("DB");
        Map<String, String> dbMap = queryStorage(ip, serverName);
        String[] dbStorageKeys = getDbStorageKeys();
        String[] storageInfo = new String[6];
        list.add(storageInfo);
        storageInfo[0] = ip + "MySQL Storage";
        storageInfo[1] = "MySQL Storage File";
        storageInfo[2] = "MySQL Storage File";
        if (dbMap.containsKey(dbStorageKeys[0])) {
            storageInfo[3] = dbMap.get(dbStorageKeys[0]); //存储目录
        } else {
            storageInfo[3] = MySQLContants.NA;
        }
        if (dbMap.containsKey(dbStorageKeys[1])) {
            storageInfo[4] = dbMap.get(dbStorageKeys[1]); //Innodb 数据主目录
        } else {
            storageInfo[4] = MySQLContants.NA;
        }
        if (dbMap.containsKey(dbStorageKeys[2])) {
            storageInfo[5] = dbMap.get(dbStorageKeys[2]); //Innodb 数据文件目录
        } else {
            storageInfo[5] = MySQLContants.NA;
        }
        return list;
    }
}
